
rm(list=ls())

loadPkg <- function(toLoad){
  for(lib in toLoad){
    if(! lib %in% installed.packages()[,1])
    { install.packages(lib, repos='http://cran.rstudio.com/') }
    suppressMessages( library(lib, character.only=TRUE) ) }
}
# Libraries
pkgs <- c("foreign", "rbounds", "Matching", "snow", "rgenoud", "haven")
loadPkg(pkgs)


library(foreign)
library(rbounds)
library(Matching)
library(snow)
library(rgenoud)


################Binary Outcome#############################

#################################################################################################################################################################################################################################################################################### NOW REPEAT WITH MITA and PRIOR MOB (dont include top-down mobiliztaion for bow as this is during treatment) #######################################################################################################################################################################################################################################
#exact matching on sendero violence, 
#replacement=false, 
#with mita and prior mob 

# Change cd to your working folder path

# Dataset
library(haven)
iv75 <- read_dta("Sensitivity.dta")
View(iv75)


set.seed(12345)
attach(iv75)
library("snow")
options(scipen = 999)
options(warn=1)

Tr <- iv75$emzone
Y <- iv75$autodefensa

X <- cbind(sendero82, provcap_dist, violence_sl_period1, mita, autodefensa_1)
BalanceMat <- cbind(sendero82, provcap_dist, violence_sl_period1, mita, autodefensa_1)
which.exact <- c(FALSE, FALSE, FALSE, FALSE, FALSE)

cl <- makeCluster(8L, type="SOCK")

genout <- GenMatch(Tr=Tr, X=X, BalanceMatrix=BalanceMat, exact=which.exact, distance.tolerance=1e-05, ties=TRUE, replace=FALSE, M=1, wait.generations=4, pop.size=10000, cluster=cl)

stopCluster(cl)

mgen2 <- Match(Y=Y, Tr=Tr, X=X, Weight.matrix=genout)

summary(genout)

MatchBalance(Tr ~ sendero82 + provcap_dist + violence_sl_period1 + mita + autodefensa_1, data=iv75, match.out=mgen2, nboots=1000)

summary(mgen2)

#Sensitivity Test
binarysens(mgen2, Gamma=25, GammaInc=.1)


############  SUBSET: SENDERO 82 = 0 -- no pscore, 
# NO exact matching on sendero violence, 
#replacement=no, 
#with mita and prior mob

iv75sub <- iv75[which(iv75$sendero82!= 1),]

set.seed(5678)
options(scipen = 999)
attach(iv75sub)
library("snow")
options(warn=1)
Tr <- iv75sub$emzone
Y <- iv75sub$autodefensa

X <- cbind(provcap_dist, violence_sl_period1, mita, autodefensa_1)
BalanceMat <- cbind(provcap_dist, violence_sl_period1, mita, autodefensa_1)
which.exact <- c(FALSE, FALSE, FALSE, FALSE)
cl <- makeCluster(8L, type="SOCK")
genout <- GenMatch(Tr=Tr, X=X, BalanceMatrix=BalanceMat, exact=which.exact, distance.tolerance=1e-05, ties=TRUE, replace=TRUE, M=1, wait.generations=4, pop.size=10000, cluster=cl)

stopCluster(cl)
mgen2 <- Match(Y=Y, Tr=Tr, X=X, Weight.matrix=genout)

summary(genout)

MatchBalance(Tr ~ provcap_dist + violence_sl_period1 + mita + autodefensa_1, data=iv75sub, match.out=mgen2, nboots=1000)

summary(mgen2)

#Sensitivity Test
binarysens(mgen2, Gamma=25, GammaInc=.1)




